package tr.edu.metu.is.mega.server.business;

import java.util.ArrayList;
import java.util.List;

import tr.edu.metu.is.mega.common.dto.ScheduleTemplateDTO;
import tr.edu.metu.is.mega.server.dao.ScheduleTemplateDAO;
import tr.edu.metu.is.mega.server.entity.Scheduletemplate;

/**
 * Class to manage schedule templates.
 */
public class ScheduleTemplateManager {

    private final ScheduleTemplateDAO dao;

    private final ScheduleTemplateDTOHelper helper;

    /**
     * Default constructor.
     */
    public ScheduleTemplateManager() {
        dao = new ScheduleTemplateDAO();
        helper = new ScheduleTemplateDTOHelper();
    }

    /**
     * Creates a schedule template.
     * 
     * @param template schedule template
     * @return schedule template
     */
    public ScheduleTemplateDTO createScheduleTemplate(ScheduleTemplateDTO template) {
        Scheduletemplate entity = new Scheduletemplate();
        helper.convertToEntity(template, entity);
        return helper.convertToDTO(dao.create(entity));
    }

    /**
     * Updates a schedule template.
     * 
     * @param template schedule template
     * @return schedule template
     */
    public ScheduleTemplateDTO updateScheduleTemplate(ScheduleTemplateDTO template) {
        Scheduletemplate entity = dao.findById(template.getPkid());
        helper.convertToEntity(template, entity);
        return helper.convertToDTO(dao.update(entity));
    }

    /**
     * Deletes a schedule template.
     * 
     * @param template schedule template
     */
    public void deleteScheduleTemplate(ScheduleTemplateDTO template) {
        dao.deleteById(template.getPkid());
    }

    /**
     * Returns schedule template count matching given criteria.
     * 
     * @param filter filter instance
     * @return schedule template count
     */
    public Long getScheduleTemplateCount(ScheduleTemplateDTO filter) {
        return 0L;
    }

    /**
     * Returns list of schedule templates.
     * 
     * @return list of schedule templates
     */
    public List<ScheduleTemplateDTO> getScheduleTemplates() {
        List<Scheduletemplate> findByExample = dao.findByExample(new Scheduletemplate(), false);
        List<ScheduleTemplateDTO> scheduleTemplateDTOs = new ArrayList<ScheduleTemplateDTO>(findByExample.size());
        for (Scheduletemplate scheduletemplate : findByExample) {
            scheduleTemplateDTOs.add(helper.convertToDTO(scheduletemplate));
        }
        return scheduleTemplateDTOs;
    }
}
